using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Specialized;
using ExtraClub.Domain.Entity;
using System.Text;
using ExtraClub.Domain.Entities;
using SubSonic;
using ExtraClub.Domain.Framework;

namespace ExtraClub.Domain {
    public partial class BaseUserControl : UserControl {

        #region Data
        public Dictionary<string, object> Items = new Dictionary<string, object>();
        private string path = null;
        #endregion
        
        private ECommand currentCommand;
        protected ECommand CurrentCommand {
            get {
                if (Request.QueryString.Count > 0 && Request.QueryString[Constants.QUERY_FROM_COMMAND] != null) {
                    currentCommand = (ECommand)Enum.Parse(typeof(ECommand), Request.QueryString[Constants.QUERY_FROM_COMMAND], true);
                } else {
                    currentCommand = ECommand.None;
                }
                return currentCommand;
            }
            set {
                currentCommand = value;
            }
        }

        private string requestedForm = "";
        protected string RequestedForm {
            get {
                if (Request.QueryString.Count > 0 && Request.QueryString["form"] != null) {
                    requestedForm = Request.QueryString["form"];
                } 
                return requestedForm;
            }
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            UIText = new IndexedProperty<string, string>(GetUITextProperty);
            UIStyle = new IndexedProperty<string, string>(GetUIStyleProperty);

            RegisterLocalAssets();
        }
        #region Private Function
        private void RegisterLocalAssets() {
            if (this.Visible) {

                if (SiteContext.Current.BasePage != null) {
                    path = TemplateControl.AppRelativeVirtualPath.Substring(0, TemplateControl.AppRelativeVirtualPath.LastIndexOf("/"));
                    DirectoryInfo di = new DirectoryInfo(Server.MapPath(path));
                    foreach (FileInfo item in di.GetFiles("*.css")) {
                        //string url = ResolveUrl(path).ToLower().Remove(0, 1) + "/" + item;
                        string url = path.Replace("~/", "").ToLower() + "/" + item;
                        SiteContext.Current.BasePage.Master.HeaderHolderMiddle.Controls.Add(new LiteralControl(string.Format(@"<link rel=""stylesheet"" type=""text/css"" href=""{0}"" />{1}", url, Environment.NewLine)));
                    }
                    foreach (FileInfo item in di.GetFiles("*.js")) {
                        if (item.Name.Contains("config")) continue;

                        string url = path.Replace("~/", "").ToLower() + "/" + item;
                        SiteContext.Current.BasePage.Master.HeaderHolderMiddle.Controls.Add(new LiteralControl(string.Format(@"<script type=""text/javascript"" src=""{0}"" ></script>{1}", url, Environment.NewLine)));
                    }
                }
            }
        }
        #endregion

        #region Public Functions
        public void ResisterCSSFile(string url) {
            SiteContext.Current.BasePage.Master.HeaderHolderBottom.Controls.Add(new LiteralControl(string.Format(@"<link rel=""stylesheet"" type=""text/css"" href=""{0}"" />{1}", url, Environment.NewLine)));
        }
        public void ResisterJSFile(string url) {
            SiteContext.Current.BasePage.Master.HeaderHolderBottom.Controls.Add(new LiteralControl(string.Format(@"<script type=""text/javascript"" src=""{0}"" ></script>{1}", url, Environment.NewLine)));
        }
        public void ResisterWSFile(string url) {
            SiteContext.Current.BasePage.Master.HeaderHolderBottom.Controls.Add(new LiteralControl(string.Format(@"<script type=""text/javascript"" src=""WSHandler.axd?services={0}&"" ></script>{1}", ResolveUrl("~/" + url.ToLower()), Environment.NewLine)));
        }
        public void ResisterCSSFile(string url, string templ) {
            string recource = string.Format(templ, string.Format(@"<link rel=""stylesheet"" type=""text/css"" href=""{0}"" />", url, Environment.NewLine));
            SiteContext.Current.BasePage.Master.HeaderHolderBottom.Controls.Add(new LiteralControl(recource));
        }
        public static Control FindControlRecursive(Control Root, string Id) {
            if (Root.ID == Id) {
                return Root;
            }
            foreach (Control Ctl in Root.Controls) {
                Control FoundCtl = FindControlRecursive(Ctl, Id);
                if (FoundCtl != null)
                    return FoundCtl;
            }
            return null;
        }

        #endregion
    }
}
